package com.niiwoo.civet.mobile.shiro;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.civet.user.dto.request.LoginAccountRequestDTO;
import com.niiwoo.civet.user.dto.request.ThirdLoginRequestDTO;
import com.niiwoo.civet.user.dto.response.LoginAccountResponseDTO;
import com.niiwoo.civet.user.service.UserLoginAccountDubboService;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.web.shiro.UserAuthPrincipal;
import com.niiwoo.tripod.web.shiro.UserDetailService;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Component;

/**
 * Created by zhangwanli on 2017/9/24.
 */
@Component
public class MobileUserDetailService implements UserDetailService {

    // @Reference(version = "1.0.0")
    // private UserBasicInfoService userBasicInfoService;

    @Reference(version = "1.0.0")
    private UserLoginAccountDubboService userLoginAccountService;

    public MobileUserDetailService() {
        System.out.println("init");
    }

    @Override
    public UserAuthPrincipal loadPrincipalByUserName(String userName) {
        LoginAccountRequestDTO requestDTO = new LoginAccountRequestDTO();
        requestDTO.setLoginMobileNo(userName);
        requestDTO.setIsCheckLoginForbid(true);
        LoginAccountResponseDTO responseDTO;
        try {
            responseDTO = userLoginAccountService.getLoginAccount(requestDTO);
        } catch (BizException e) {
            throw new MobileUserDetailException(e);
        }
        if (responseDTO == null) {
            return null;
        }
        UserAuthPrincipal authPrincipal = new UserAuthPrincipal();
        authPrincipal.setPrincipal(responseDTO.getLoginMobileNo());
        authPrincipal.setCredentials(responseDTO.getPassword());
        authPrincipal.setSalt(responseDTO.getSlat());
        authPrincipal.setUserId(responseDTO.getUserId());
        return authPrincipal;


    }

    @Override
    public UserAuthPrincipal loadPrincipalByOpenId(String openId, Byte platformId) {
        ThirdLoginRequestDTO requestDTO = new ThirdLoginRequestDTO();
        requestDTO.setOpenId(openId);
        requestDTO.setPlatformId(platformId);
        LoginAccountResponseDTO responseDTO;
        try {
            responseDTO = userLoginAccountService.getAccountInfoByOpenId(requestDTO);
        } catch (BizException e) {
            throw new MobileUserDetailException(e);
        }
        if (responseDTO == null) {
            return null;
        }
        UserAuthPrincipal authPrincipal = new UserAuthPrincipal();
        authPrincipal.setPrincipal(responseDTO.getLoginMobileNo());
        authPrincipal.setCredentials(platformId.toString());
        authPrincipal.setSalt(responseDTO.getSlat());
        authPrincipal.setUserId(responseDTO.getUserId());
        return authPrincipal;


    }

    @Override
    public List<String> queryRoles(String userId) {
        return Collections.emptyList();
    }

    @Override
    public List<String> queryPermissions(String userId) {
        return Collections.emptyList();
    }

}
